package logic

import (
	"context"

	"github.com/eeppee_admin/clouddisk/service/internal/svc"
	"github.com/eeppee_admin/clouddisk/service/internal/types"
	"github.com/eeppee_admin/clouddisk/service/helper"

	"github.com/zeromicro/go-zero/core/logx"
)

type AuthorizationRefreshLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAuthorizationRefreshLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AuthorizationRefreshLogic {
	return &AuthorizationRefreshLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AuthorizationRefreshLogic) AuthorizationRefresh(req *types.AuthorizationRefreshRequest,auth string) (resp *types.AuthorizationRefreshResponse, err error) {
	uc, err := helper.AnalyzeToken(auth)
	if err != nil {
		return
	}
	token, err := helper.GenerateToken(uc.Id, uc.Identity, uc.Name, l.svcCtx.Config.Jwt.TokenExpire)
	if err != nil {
		return
	}

	refreshToken, err := helper.GenerateToken(uc.Id,uc.Identity, uc.Name, l.svcCtx.Config.Jwt.RefreshTokenExpire)
	if err != nil {
		return
	}

	resp = new(types.AuthorizationRefreshResponse)
	resp.Token = token
	resp.RefreshToken = refreshToken
	return
}
